package view;


import javax.swing.JPanel;
import javax.swing.JButton;

import behavior.Parameters;

import java.lang.reflect.*;

import individual.*;
import utilities.Reflection;
import utilities.Vector;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import debugger.*;


/**
 * This is a Jpanel that holds the buttons to add a Boid
 * This is a class because all Boids can be added to the GUI through the same Action Listener
 * because the constructor requires the same parameters
 * 
 * This is potentially extensible if other MovingEntities have the same parameters.
 * If not, this class provides as a model for another JPanel class that can serve as a 
 * "factory" of sorts that can be constructed from the same parameter types.
 * 
 * @author Ga-Young Joung
 */
@SuppressWarnings("serial")
public class BoidsPanel extends JPanel {
	
    public Canvas myView;
    public RangeSlider myNumToCreate;
    public Parameters myParameters;
    public Debugger myDebugger;

    /**
     * Create a button panel to act on the given target.
     * 
     * @param target canvas upon which commands act on 
     * @param input component that provides additional input to commands
     */
    public BoidsPanel (Canvas view, RangeSlider numToCreate, Parameters parameters, Debugger debugger)
    {
        myView = view;
        myNumToCreate = numToCreate;
        myParameters = parameters;
        myDebugger = debugger;
    }
    
    /**
     * Listener for buttons to create Boids
     * @author ga-youngjoung
     */
    public class AddBoidTypeButtonListener implements ActionListener {
    	
    	private String myClassName;
    	
    	public AddBoidTypeButtonListener(String className) {
    		myClassName = className;
    	}
    	
    	public void actionPerformed (ActionEvent e)
		{
				Dimension bounds = myView.getSize();
    			int inds = myNumToCreate.getValue();
    			for (int i = 0; i < inds; i++) {
    				int size = 16;
    				MovingEntity indiv = (MovingEntity) Reflection.createInstance(
    						"individual."+myClassName, 
    						new Vector(myView.nextIntInRange(size /2, bounds.width - size / 2), 
    								myView.nextIntInRange(size / 2, bounds.width - size / 2)),
    						new Dimension(size, size),
	       	                new Vector(myView.nextNonZeroIntInRange(-5, 5),
	       	                		myView.nextNonZeroIntInRange(-5, 5)),
	       	                myView.getEnvironment(),
	       	                myParameters,
	       	                myDebugger);
	   				myView.getEnvironment().add(indiv);
    	        }
    			myView.repaint();
		}
    }
    
    /**
     * adds the button for adding the given class of Boid
     * @param className - name of the Boid to add
     */
    public void addButton (String className) {
    	JButton addButton = new JButton("Add " + className);
    	addButton.addActionListener(new AddBoidTypeButtonListener(className));
    	add(addButton);
	}
    
}
